<?php

namespace app\api\model\wanlshop;

use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{
    use SoftDelete;
	
    // 表名
    protected $name = 'wanlshop_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    protected $type = [
        'id' => 'string',  // 确保BIGINT字段作为字符串处理
    ];
    protected $casts = [
        'id' => 'string',
    ];
	// getImagesAttr
	public function getImagesAttr($value)
	{	
		return $value ? explode(',', $value) : [];
	} 
	
	// SKU
	public function getSkuAttr($value, $data)
	{	
		$list = GoodsSku::where(['goods_id'=>$data['id'], 'stock'=>['>',0], 'state'=> ['=',0]])->field('id,thumbnail,difference,price,market_price,stock,state,weigh,sn,sales')->select();

        if($data['is_integral'] == 1){
            foreach ($list as $value){
                $valueData = $this->useScore($value->price,$data['maximum_proportion']);
                $value->useScore = $valueData['useScore'];
                $value->newPrice = $valueData['price'];
            }
        }

        return $list;
	}
    public function useScore($price,$maximum_proportion = 0)
    {
        //获取1积分抵扣金额
        $config = get_addon_config('wanlshop');
        $deduct_amount = $config['config']['deduction_amount']; // 1积分抵扣金额
        // 积分可抵扣金额的百分比
        $maximum_proportion = $maximum_proportion / 100;
        // 最大可以使用积分抵扣的金额
        $max_deductible_amount = min($price, $price * $maximum_proportion);
        // 需要使用的积分数 = 最大可抵扣金额 ÷ 每1积分可抵扣的金额
        $required_score = ceil($max_deductible_amount / $deduct_amount);
        // 实际抵扣金额 = 所使用的积分 * 每1积分可抵扣的金额
        $actual_deduct_amount = $required_score * $deduct_amount;
        // 保证抵扣后剩余金额是非负，并保留两位小数
        $remaining_price = number_format($price - $actual_deduct_amount, 2, '.', '');
        $data = [
            'useScore'=>$required_score,
            'price'=>$remaining_price
        ];
        return $data;
    }
	// SPU
	public function getSpuAttr($value, $data)
	{
	    $list = GoodsSpu::where('goods_id',$data['id'])->field('id,name,item')->select();
		return $list;
	}
	
	// 获取评论
	public function getCommentListAttr($value, $data)
	{	
		$comment = new GoodsComment;
		$where = [
			'goods_id' => $data['id'],
			'order_type' => 'goods'
		];
		$list = $comment
			->where($where)
			->order('createtime desc')
			->field('id,user_id,content,suk,images,score')
			->limit(1) // 默认展示几条评论
			->select();
		foreach ($list as $row) {
		    $row->user->visible(['id','nickname','avatar']);
		}
		return [
			'figure' => $comment->where($where)->where('images','neq', '')->count(), //有图
			// 'tag' => array_count_values($comment->where($where)->limit(100)->column('tag')), //评论标签
			'data' => $list 
		];
	}
	
	
	// 类目属性 格式化
	public function getCategoryAttributeAttr($value)
	{	
		return json_decode($value,true);
	}
	
	// 品牌
	public function brand()
	{
	    return $this->belongsTo('app\api\model\wanlshop\Brand', 'brand_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	// 运费模板
	public function freight()
	{
	    return $this->belongsTo('app\api\model\wanlshop\ShopFreight', 'freight_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	// 运费模板
	public function freightdata()
	{
	    return $this->belongsTo('app\api\model\wanlshop\ShopFreightData', 'freight_id', 'freight_id', [], 'LEFT')->setEagerlyType(0);
	}
	
	// 店铺
	public function shop()
	{
	    return $this->belongsTo('app\api\model\wanlshop\Shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	// 类目
	public function category()
	{
	    return $this->belongsTo('app\api\model\wanlshop\Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	// 店铺类目
	public function shopsort()
	{
	    return $this->belongsTo('app\api\model\wanlshop\ShopSort', 'shop_category_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	
	
}
